home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / SourceCode / OOP_Course / Slides / 7.PostScript-2.txt next >
Text File  |  1992-12-19  |  9KB  |  325 lines

  1. PostScript Tutorial                                                                                              Michael D. Lore
  2.                     University of Houston
  3.                     Department of Electrical Engineering
  4.  
  5. I. Introduction
  6.  
  7.     A. PostScript: A Page Description Langauge
  8.         
  9.         1. Can write a variety of text in many different typestyles.  Can draw geometric figures,
  10.         and draw digitized images.  Provides support for color too.
  11.         
  12.         2. The imaging model allows one to build up where the ink goes on a page, and then
  13.         display the page.  PostScript has a current page, a current path, and a current clipping
  14.         path.  Operators exists for defining the current path and current clipping path.  Other
  15.         operators allow you to stroke or fill the path.
  16.         
  17.         3. PostScript uses a standard mathematical coordinate system, and allows coordinate
  18.         system operations like rotation and translation.
  19.         
  20.     B. PostScript: A Programming Language
  21.     
  22.         1. The portions of PostScript not devoted to graphics make up a general programming
  23.         language based on postfix notation and a stack.
  24.         
  25.         2. The PostScript stack is where most data and program code reside.  Postfix notation
  26.         is used because it most closely parallels the operation of a stack.
  27.         
  28.         3. PostScript supports several data types, like integers, reals, and dictionaries.
  29.         
  30. II. Using the PostScript Langauge
  31.  
  32.     A. The Stack
  33.     
  34.         1. A stack is a type of last-in, first-out list.  A good example is an auto change dispenser,
  35.         in which coins may be inserted and removed from the top.  The last coin placed in is
  36.         always the first coin removed.
  37.         Putting an item on top of the stack is called "pushing it onto the stack."
  38.         Removing an item from the top of the stack is called "popping it off of the stack."
  39.         
  40.         2. In PostScript, an in-line item is pushed onto the stack by writing it:
  41.         
  42.             23 44 55.5
  43.             
  44.         Places 23, then 44, and then 55.5 onto the top of the stack.  When operators are 
  45.         placed on the stack, they are immediately thereafter executed.  You might think of
  46.         an execution for every item ever placed on the stack: the operation defined for
  47.         numbers is to place them on the stack. 
  48.         
  49.             23  44  add  --->  67
  50.             
  51.     B. Postfix Notation
  52.     
  53.         1. Normal arithmetic operators follow a notation convention in which the operator is
  54.         placed in between the two items being operated on:
  55.         
  56.             3 + 4
  57.             
  58.         With Postfix notation, the operator is placed after the two arguments:
  59.         
  60.             3  4  +
  61.             
  62.         2. Normal arithmetic suffers from an ill-defined way to determine precedence between
  63.         operations.  4 + 5 * 7 must be evaluated carefully because * has priority over +.
  64.         
  65.         With postfix notation, however, there is no such problem.  4  5  7  *  + can be easily 
  66.         and almost carelessly evaluated because each operator always acts upon the previous 
  67.         two arguments.  This method is very powerful for a stack-based system, because
  68.         operators and arguments can be immediately evaluated as they are popped off the
  69.         stack.
  70.         
  71.     C. PostScript Examples
  72.  
  73.         1. Arithmetic
  74.             5  3 add            -->    8        like 5 + 3
  75.             99 1 sub            -->    98        like 99 - 1
  76.             3 3 3 add mul        -->    18        like (3 + 3) * 3
  77.         
  78.         2. Stack operators
  79.         
  80.             3  44  55 clear        -->    
  81.             4 dup            -->    4 4
  82.             3  55  pop        -->    3
  83.             33  44 exch        -->    44  33
  84.         
  85. III. Drawing
  86.  
  87.     A. Introduction
  88.     
  89.         1. Drawing consists of two steps: defining a path to follow and then stroking or filling that
  90.         path.  My examples will be of drawing a box.  PostScript features will be desribed along
  91.         the way.  
  92.         
  93.         2. The YAP PostScript previewer is used for these examples, so that we can
  94.         see what is going on along the way.
  95.     
  96.     B. A Box
  97.         
  98.         1. Draw a box
  99.         newpath
  100.             270 360 moveto
  101.             0 144 rlineto            % relative line to.
  102.             144 0 rlineto
  103.             0 -144 rlineto
  104.             -144 0 rlineto
  105.             12 setlinewidth            % set width of drawn line
  106.         stroke                    % finally stroke (as if with a pen) the outline
  107.         showpage
  108.         
  109.         2. Draw a better box
  110.         newpath
  111.             270 360 moveto
  112.             0 144 rlineto
  113.             144 0 rlineto
  114.             0 -144 rlineto
  115.             closepath                % closepath closes a path nicely
  116.             12 setlinewidth
  117.         stroke
  118.         showpage
  119.         
  120.         3. Fill the box
  121.         newpath
  122.             270 360 moveto
  123.             0 144 rlineto
  124.             144 0 rlineto
  125.             0 -144 rlineto
  126.             closepath
  127.         fill                        % fill the outline instead
  128.         showpage
  129.         
  130.         4. Use a different color
  131.         newpath
  132.             270 360 moveto
  133.             0 144 rlineto
  134.             144 0 rlineto
  135.             0 -144 rlineto
  136.             closepath
  137.         0.5 setgray                % use a different gray level
  138.         fill
  139.         showpage
  140.         
  141.         5.  Draw 3 overlapping boxes
  142.         newpath                    % black box
  143.             252 324 moveto
  144.             0 144 rlineto
  145.             144 0 rlineto
  146.             0 -144 rlineto
  147.             closepath
  148.         0 setgray
  149.         fill
  150.  
  151.         newpath                    % dark gray box
  152.             270 360 moveto
  153.             0 144 rlineto
  154.             144 0 rlineto
  155.             0 -144 rlineto
  156.             closepath
  157.         0.4 setgray
  158.         fill
  159.  
  160.         newpath                    % light gray box
  161.             288 396 moveto
  162.             0 144 rlineto
  163.             144 0 rlineto
  164.             0 -144 rlineto
  165.             closepath
  166.         0.8 setgray
  167.         fill
  168.  
  169.         showpage
  170.         
  171.         6. Why write the code 3 times as above? We can define procedures.
  172.         
  173.         /X 270 def        % variable stores x location
  174.         /Y 360 def        % variable stores y location
  175.         
  176.         % --- procedure to draw a box.  Stack: -  -->  -
  177.         /box {
  178.              144 0 rlineto
  179.              0 144 rlineto
  180.              -144 0 rlineto
  181.              closepath
  182.         } def
  183.         
  184.         % --- main program
  185.         newpath                        % black box
  186.             X 18 sub Y 36 sub moveto box 0 setgray
  187.         fill
  188.         newpath                        % dark gray box
  189.             X Y moveto box 0.4 setgray
  190.         fill
  191.         newpath                        % light gray box
  192.             X 18 add Y 36 add moveto box 0.8 setgray
  193.         fill
  194.         
  195.         showpage
  196.         
  197.         7. We can also pass parameters on the stack.  The inch procedure converts
  198.         from inches to the PostScript coordinates.  We also redefine the box procedure
  199.         to allow us to tell where to draw it, what color, and how big.
  200.         
  201.         % --- procedure to convert from inches to 1/72 inch units. Stack: inches --> psunits
  202.         /inch {
  203.             72 mul
  204.         } def
  205.         
  206.         % --- procedure to draw a box. Stack: size color x y -->  -
  207.         /box {
  208.             newpath
  209.                 moveto
  210.                 setgray
  211.                 /size exch def
  212.                 size 0 rlineto
  213.                 0 size rlineto
  214.                 size neg 0 rlineto
  215.                 closepath
  216.         } def
  217.         
  218.         % --- main program
  219.         2 inch 0 1 inch 1 inch box fill            % black box
  220.         2.5 inch 0.4 1.5 inch 1.5 inch box fill    % dark gray box
  221.         3 inch 0.8 2.0 inch 2.0 inch box fill        % light gray box
  222.         3 inch 0 2.0 inch 2.0 inch box 
  223.             8 setlinewidth stroke            % border light gray box
  224.         
  225.         showpage
  226.         
  227.         8. Generally, passing parameters on the stack can be done as follows:
  228.         
  229.         If the call to the procedure is:   parm1 parm2 parm3 procedureName
  230.         
  231.         Then:
  232.         
  233.         /procedureName {
  234.             /parm3 exch def
  235.             /parm2 exch def
  236.             /parm1 exch def
  237.             .... use variables parm1, parm2, and parm3
  238.         } def
  239.         
  240.         is the procedure.
  241.         
  242.         9. Writing text
  243.         To write text, we must perform the following operations:
  244.             - Find the font of the given name from the font dictionary.
  245.             - Scale the font to the appropriate size.
  246.             - Set the font by popping it off the stack and establishing it as the current font.
  247.         
  248.         /inch {72 mul } def
  249.         /font1 /Times-Roman findfont 100 scalefont def                    % save the found, scaled font
  250.         /font2 /Times-BoldItalic findfont 80 scalefont def                % save the found, scaled font
  251.         2 inch 2 inch moveto            % write 'HELLO'
  252.         0 setgray
  253.         font1 setfont (HELLO) show
  254.         3 inch 1.7 inch moveto            % write 'there'
  255.         0.3333 setgray
  256.         font2 setfont (there) show
  257.         showpage
  258.         
  259.         10. Final Example: a NeXT Button
  260.         
  261.         % draw button - xloc,yloc should be lower left corner of button. 
  262.         %  stack: xloc yloc xsize ysize -> -
  263.         /white 1.0 def
  264.         /lightGray 0.66666 def
  265.         /darkGray 0.33333 def
  266.         /black 0.0 def
  267.         /drawButton {
  268.             gsave
  269.             /ysize exch def
  270.             /xsize exch def
  271.             /yloc exch def
  272.             /xloc exch def
  273.             1 setlinewidth
  274.             2 setlinecap
  275.                 newpath
  276.                     xloc yloc moveto
  277.                     0 ysize rlineto
  278.                     xsize 0 rlineto
  279.                 white setgray stroke
  280.                 newpath 
  281.                     xloc yloc moveto
  282.                     xsize 0 rlineto
  283.                     0 ysize rlineto
  284.                 black setgray stroke
  285.                 newpath
  286.                     1 xloc add 1 yloc add moveto
  287.                     xsize 2 sub 0 rlineto
  288.                     0 ysize 2 sub rlineto
  289.                 darkGray setgray stroke
  290.                 newpath
  291.                     1 xloc add 1 yloc add moveto
  292.                     xsize 2 sub 0 rlineto
  293.                     0 ysize 2 sub rlineto
  294.                     2 xsize sub 0 rlineto
  295.                 closepath lightGray setgray fill        
  296.             grestore
  297.         } def
  298.         /inch { 72 mul } def
  299.  
  300.         % main program
  301.         /Times-Roman findfont 30 scalefont setfont
  302.         
  303.         % draw background
  304.         newpath
  305.             0 0 moveto
  306.             0 4 inch rlineto
  307.             4 inch 0 rlineto
  308.             0 -4 inch rlineto
  309.             closepath
  310.             lightGray setgray 
  311.         fill
  312.  
  313.         % draw button
  314.         2 inch 2 inch 1.4 inch 0.5 inch drawButton
  315.         0 setgray
  316.         2.1 inch 2.1 inch moveto
  317.         (Button) show
  318.         showpage
  319.  
  320. IV. Conclusion
  321.  
  322.     PostScript is versatile enough to provide all drawing needs, and fast enough on the NeXT
  323.     for good application performance.  It allows a true "what you see is what you get" system,
  324.     and developers need not worry about supporting various different kinds of hardware.        
  325.